﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Cormant.Mvc.Web
{
    public class MvcGridViewLinkColumn : MvcGridViewBoundColumn
    {
        public string LinkIDFormat { get; set; }
        public string NavigateUrl { get; set; }
        public string Text { get; set; }

        public override MvcTableCell CreateTableCell(MvcTable table, HtmlHelper html, object dataItem)
        {
            // create the default TableCell...
            MvcTableCell cell = base.CreateTableCell(table, html, dataItem);

            // construct the ID of the Link...
            string linkID;
            if (!string.IsNullOrEmpty(this.LinkIDFormat))
                linkID = string.Format(this.LinkIDFormat, table.Rows.Count - 1);
            else
                linkID = string.Format("{0}_{1}", table.Id, table.Rows.Count - 1);

            // initialize the Link...
            MvcLink link = new MvcLink(html, linkID);
            link.NavigateUrl = this.NavigateUrl;
            string dataFieldValue = this.GetDataItemFieldValue(dataItem);
            link.Text = !string.IsNullOrEmpty(dataFieldValue) ? dataFieldValue : this.Text;

            cell.Controls.Add(link);
            return cell;
        }
    }
}
